package cn.gongwebo.payview.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;

import java.util.List;

import cn.gongwebo.payview.R;

/**
 * Created by GongWenBo on 2017/12/8.
 */

public class KeyView extends KeyboardView implements KeyboardView.OnKeyboardActionListener {

    private Context mContext;
    private int KEYCODE_EMPTY = -10;
    private Drawable mDeleteDrawable;
    private Canvas   mCanvas;
    private static final String TAG = "KeyView";
    private int mSpace;

    public KeyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView(attrs);
    }

    private void initView(AttributeSet attrs) {
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.KeyView);
        mDeleteDrawable = typedArray.getDrawable(R.styleable.KeyView_keyView_delete);
        // TODO: 2017/12/11 获取xml中的按键布局
        Keyboard keyboard = new Keyboard(mContext, R.xml.keyboard);
        setKeyboard(keyboard);
        setEnabled(true);
        setPreviewEnabled(true);
        setOnKeyboardActionListener(this);
        // TODO: 2017/12/11  按键的时候弹出小窗口
        setPreviewEnabled(false);

        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        mSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1.5f, dm);
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        List<Keyboard.Key> keys = getKeyboard().getKeys();
        for (Keyboard.Key key : keys) {
            //绘制空白键
            if (key.codes[0] == KEYCODE_EMPTY) {
                drawKeyBackGround(key, canvas);
            } else if (key.codes[0] == Keyboard.KEYCODE_DELETE) {
                //绘制删除键背景
                drawKeyBackGround(key, canvas);
                //                //绘制按键图片
                drawkeyDelete(key, canvas);
            }
        }
        mCanvas = canvas;
    }

    private void drawKeyBackGround(Keyboard.Key key, Canvas canvas) {
        Drawable drawable = mContext.getResources().getDrawable(R.drawable.keyboard_empty);
        // 选择器效果
        int[] state = key.getCurrentDrawableState();
        drawable.setState(state);
        // TODO: 2017/12/21 减去key的间距，很难控制，而且有按键问题
        //        drawable.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
        drawable.setBounds(key.x, key.y + mSpace, key.x + key.width, key.y + key.height + mSpace);
        drawable.draw(canvas);
    }

    private void drawkeyDelete(Keyboard.Key key, Canvas canvas) {
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, dm);
        int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 11, dm);
        int[] state = key.getCurrentDrawableState();
        mDeleteDrawable.setState(state);
        mDeleteDrawable.setBounds(key.x + width, key.y + height, key.x + key.width - width, key.y + key.height - height);
        mDeleteDrawable.draw(canvas);
    }

    //回调接口
    public interface OnKeyPressListener {
        //添加数据回调
        void onInertKey(String text);

        //删除数据回调
        void onDeleteKey();
    }

    private OnKeyPressListener mOnkeyPressListener;

    public void setOnKeyPressListener(OnKeyPressListener li) {
        mOnkeyPressListener = li;
    }

    @Override
    public void onPress(int primaryCode) {
        Log.d(TAG, "onPress: " + primaryCode);
    }

    @Override
    public void onRelease(int primaryCode) {
        Log.d(TAG, "onRelease: " + primaryCode);
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        Log.d(TAG, "onKey: " + primaryCode);
        if (primaryCode == Keyboard.KEYCODE_DELETE && mOnkeyPressListener != null) {
            if (mOnkeyPressListener != null) {
                mOnkeyPressListener.onDeleteKey();
            }
        } else if (primaryCode != KEYCODE_EMPTY) {
            if (mOnkeyPressListener != null) {
                Log.d(TAG, "数字: " + Character.toString((char) primaryCode));
                mOnkeyPressListener.onInertKey(Character.toString((char) primaryCode));
            }
        }
    }

    /**
     * 按键声音
     *
     * @param keyCode
     */
    private void playClick(int keyCode) {
        AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
    }

    @Override
    public void onText(CharSequence text) {

    }

    @Override
    public void swipeLeft() {

    }

    @Override
    public void swipeRight() {

    }

    @Override
    public void swipeDown() {

    }

    @Override
    public void swipeUp() {

    }
}
